package io.renren.modules.banner.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.modules.banner.dao.BannerDao;
import io.renren.modules.banner.entity.BannerEntity;
import io.renren.modules.banner.service.BannerService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;

/**
 * Created by dell on 2021/8/11.
 */
@Service("bannerService")
public class BannerServiceImpl extends ServiceImpl<BannerDao, BannerEntity> implements BannerService {
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String name = (String)params.get("name");
        IPage<BannerEntity> page = this.page(
                new Query<BannerEntity>().getPage(params),
                new QueryWrapper<BannerEntity>().like(StringUtils.isNotBlank(name), "name", name)
        );
        return  new PageUtils(page);
    }

    @Override
    @Transactional
    public void saveBanner(BannerEntity banner) {
        banner.setCreateTime(new Date());
        this.save(banner);
    }

    @Override
    public void deleteBatch(Long[] bannerIds) {
        this.removeByIds(Arrays.asList(bannerIds));
    }

    @Override
    @Transactional
    public void update(BannerEntity banner) {
        this.updateById(banner);
    }
}
